package com.itheima.dao;

import com.itheima.pojo.Member;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;
import java.util.Map;

/**
 * 会员持久层接口
 */
public interface MemberDao {

    /**
     * 根据电话查询会员
     *
     * @param telephone 电话
     * @return 会员
     */
    @Select("select * from t_member where phoneNumber=#{telephone}")
    Member findByTelephone(String telephone);

    /**
     * 添加会员  查询添加的主键
     *
     * @param member 会员
     */
    @Insert("insert into t_member (`name`,`sex`,`idCard`,`phoneNumber`,`regTime`) " +
            "values(#{name},#{sex},#{idCard},#{phoneNumber},#{regTime}) ")
    @SelectKey(before = false, statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(Member member);

    /**
     * 查询当前月份的总会员人数
     *
     * @param month 月份
     * @return 总会员数
     */
    @Select("select count(id) from t_member where regTime <=#{value}")
    Integer findMemberCountByBeforeDate(String month);

    /**
     * 查询每天的总会员人数
     *
     * @return 日期 - 总会员数
     */
    @Select("select m1.regTime regTime , SUM(m2.count) as 'count' from \n" +
            "(SELECT regTime, COUNT(1) 'count' FROM t_member group by regTime) m1,\n" +
            "(SELECT regTime, COUNT(1) 'count' FROM t_member group by regTime) m2\n" +
            "where m1.regTime >= m2.regTime  group by m1.regTime")
    List<Map<String, String>> getMemberReportByEveryday();

    /**
     * 查询本日的预约总人数
     *
     * @param today 本日日期
     * @return 本日预约总人数
     */
    @Select("select count(1) from t_member where regTime=#{today}")
    Integer findMemberCountByDate(String today);

    /**
     * 查询总会员数
     *
     * @return 总会员数
     */
    @Select("select count(1) from t_member ")
    Integer findMemberTotal();

    /**
     * 查询本周会员数
     *
     * @param thisWeekMonday 本周
     * @return 本周会员数
     */
    @Select("select count(1) from t_member where regTime>=#{thisWeekMonday} ")
    Integer findMemberCountByThisWeek(String thisWeekMonday);

    /**
     * 本月会员数
     *
     * @param firstDay4ThisMonth 本月
     * @return 本月会员数
     */
    @Select("select count(1) from t_member where regTime>=#{firstDay4ThisMonth}")
    Integer findMemberCountByThisMonth(String firstDay4ThisMonth);

}
